package jeecg.business.job;

import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import jeecg.business.entity.email.NmsSendMailEntity;
import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.service.email.NmsSendMailServiceI;
import jeecg.business.service.enter.EnterServiceI;
import jeecg.business.service.keyinfo.KeyinfoServiceI;
import jeecg.business.util.SmsUtil;
import jeecg.business.vo.EmailAuthenticator;

import org.jeecgframework.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SmsTask {
	
	@Autowired
	EnterServiceI enterService;
	@Autowired
	KeyinfoServiceI keyinfoService;
	@Autowired
	NmsSendMailServiceI mailService;
	/**
	 * 
	 * <p>
	 * 方法简要描述: 每天的早上9点和下午4点
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 */
	@Scheduled(cron = "0 0 8,10,12,14,16,18 * * ?")
//	@Scheduled(cron="0 0/1 * * * ?")
	public void sendEmail() {
		List<EnterEntity> enterList = enterService.list();
		if (enterList != null && enterList.size() > 0) {
			for (EnterEntity enter : enterList) {
				sendMail4Enter(enter);
			}
		}
	}

	void sendMail4Enter(EnterEntity enter) {
		KeyinfoEntity ys = new KeyinfoEntity();
		ys.setSfys("1");
		ys.setQyid(enter.getId());
		if(enter.getLoginTime() == null) enter.setLoginTime(enter.getCreatetime());
		int yscount = keyinfoService.countAfterTime(enter.getLoginTime(), ys);
		List<KeyinfoEntity> yslist = null;
		if (yscount > 0)
			yslist = keyinfoService.listAfterTime(enter.getLoginTime(), ys, 10);

		KeyinfoEntity fm = new KeyinfoEntity();
		fm.setSffm("1");
		fm.setQyid(enter.getId());
		int fmcount = keyinfoService.countAfterTime(enter.getLoginTime(), fm);
		List<KeyinfoEntity> fmlist = null;
		if (fmcount > 0)
			fmlist = keyinfoService.listAfterTime(enter.getLoginTime(), fm, 10);

		if (yscount > 0 || fmcount > 0) {
			try {
				// 发送邮箱
				NmsSendMailEntity mail = mailService.random();
				if (mail != null && StringUtil.isNotEmpty(enter.getEmail()) && "1".equals(enter.getUseemail())) {
					String html = "亲爱的" + enter.getName() + "：<br>";
					if (fmcount > 0) {
						html += "互联网舆情监测系统发现了您单位疑似负面舆情有" + fmcount + "条，其他分别是：<br>";
						for (KeyinfoEntity t : fmlist) {
							html += (fmlist.indexOf(t) + 1) + "、<a href='"+t.getYurl()+"' target='_blank' >" + t.getTitle() + "</a><br>";
						}
					}
					if (yscount > 0) {
						html += "互联网舆情监测系统发现了您单位可疑舆情有" + yscount + "条，其他分别是：<br>";
						for (KeyinfoEntity t : yslist) {
							html += (yslist.indexOf(t) + 1) + "、<a href='"+t.getYurl()+"' target='_blank' >" + t.getTitle() +  "</a><br>";
						}
					}

					send(mail, enter.getEmail(), html);
					if (mail.getFscs() == null)
						mail.setFscs(0);
					mail.setFscs(1 + mail.getFscs());
					mailService.updateEntitie(mail);
				}
				
				if(StringUtil.isNotEmpty(enter.getMobile()) && "1".equals(enter.getUsephone())){
					String content = "";
					if (fmcount > 0)
						content = "互联网舆情监测系统发现了您单位疑似负面舆情有" + fmcount + "条";
					if (yscount > 0) 
						content += ",互联网舆情监测系统发现了您单位可疑舆情有" + yscount + "条";
					if(StringUtil.isNotEmpty(content)){
						content += ".";
						SmsUtil.sendSms(enter.getMobile(), content);
					}
				}
				// 发送短信
				enter.setLoginTime(new Date());
				enterService.updateEntitie(enter);
			} catch (Exception e) {
				e.printStackTrace();
			}

			
		}

	}

	public static void send(NmsSendMailEntity mail, String to, String message) throws Exception {
		// String subject = "在线活动激活";
		String subject = "互联网舆情监测";
		Properties properties = new Properties();
		properties.put("mail.smtp.host", "smtp.qq.com");
		properties.put("mail.smtp.port", 25);
		properties.put("mail.smtp.auth", true);
		Authenticator authenticator = new EmailAuthenticator(mail.getYxdz(), mail.getYxmm());
		javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
		MimeMessage mailMessage = new MimeMessage(sendMailSession);
		mailMessage.setFrom(new InternetAddress(mail.getYxdz()));
		// Message.RecipientType.TO属性表示接收者的类型为TO
		mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		mailMessage.setSubject(subject, "UTF-8");
		mailMessage.setSentDate(new Date());
		// MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
		Multipart mainPart = new MimeMultipart();
		// 创建一个包含HTML内容的MimeBodyPart
		BodyPart html = new MimeBodyPart();
		html.setContent(message.trim(), "text/html; charset=utf-8");
		mainPart.addBodyPart(html);
		mailMessage.setContent(mainPart);
		Transport.send(mailMessage);
	}
}
